<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="项目等级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectLevel">
              <a-input   v-model="model.projectLevel" placeholder="请选择项目等级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="绩点类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gradePointType">
              <a-select   v-model="model.gradePointType" placeholder="请选择绩点类别"  @change="gradePointChange">
                <a-select-option v-for="codeInfo in gradePointTypeList" :value="codeInfo.code" :key="codeInfo.code" :title="codeInfo.name" >{{ codeInfo.name }}</a-select-option>
              </a-select>
            </a-form-model-item>

          </a-col>
          <a-col :span="24">
            <a-form-model-item label="转换系数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scoreRatio">
              <a-input-number :min='0.0001' :max='10000' v-model="model.scoreRatio"  placeholder="请输入转换系数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
         <a-col :span="24">
            <a-form-model-item label="基础奖励" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="baseReward">
              <a-input-number :min='0' :precision="2" v-model="model.baseReward"  placeholder="请输入基础奖励" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
 import {codeInfoList} from '@/api/codeInfo'
  export default {
    name: 'HteReportPgScoreForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           projectLevel: [
              { required: true, message: '请输入项目等级!'},
           ],
           gradePointType: [
              { required: true, message: '请输入绩点类别!'},
           ],
           scoreRatio: [
              { required: true, message: '请输入转换系数!'},
           ],
           baseReward: [
              { required: true, message: '请输入基础奖励!'},
           ],
        },
        url: {
          add: "/reportCenter/add",
          edit: "/reportCenter/edit",
          queryById: "/reportCenter/queryById"
        },
        gradePointTypeList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.initDics()
    },
    methods: {
       initDics(){
        codeInfoList({type:'工程项目业绩报表绩点类别',pageSize:-1,isdel: 0}).then((res) => {
          if (res.code == 200) {
            this.gradePointTypeList = res.result.records  
          } else {
            this.$message.warning('操作异常:' + res.message)
          }
        })
        .catch((res) => {
          this.$message.warning('操作异常:' + res.message)
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      gradePointChange(value,option){
        this.model.gradePointTypeName = option.componentOptions.propsData.title
      },
    }
  }
</script>